﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;
using System.Data;

namespace Presentation
{
	public partial class PersonalPreferences : System.Web.UI.Page
	{
		private List<ISkill> skills;
		private List<ITask> tasks;
		private ITeacher me;
		private DropDownList[] starArray;

		private static IEngine engine = Engine.Instance();

		protected void Page_Load( object sender, EventArgs e )
		{
			if( !engine.IsTeacherSettingsEnabled )
			{
				Response.Redirect( "~/Teacher/NoPreferences.aspx" );
			}
		}


		protected void Button1_Click( object sender, EventArgs e )
		{
			try
			{
				if( !engine.IsTeacherSettingsEnabled )
				{
					Response.Redirect( "~/Teacher/Error.aspx" );
				}
				else
				{
					List<ILike> lList = new List<ILike>();
					for( int i = 0; i < tasks.Count; i++ )
					{
						if( starArray[i] != null )
						{
							int star = starArray[i].SelectedIndex;
							lList.Add( engine.ConstructLike( tasks[i], star ) );
						}
					}
					engine.UpdatePreferences( me, me.Skills, lList );
					Response.Redirect( "~/Teacher/TeacherDefault.aspx?message=ok" );
				}
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}

		protected void InitComponents()
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsTeacher( visitorName ) )
			{
				if( engine.IsAdmin( visitorName ) )
				{
					Response.Redirect( "~/Teacher/AdminProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}
			}
			else
			{
				skills = engine.GetSkills();
				tasks = engine.GetTasks( true );
				me = engine.GetTeacherProfile( User.Identity.Name );
				starArray = new DropDownList[tasks.Count];

				Table Table1 = new Table();

				int colNbr = 5;
				Table1.BorderStyle = BorderStyle.Groove;
				Table1.GridLines = GridLines.Both;
				Table1.CellPadding = 15;

				TableRow headerRow = new TableRow();
				for( int i = 0; i < colNbr; i++ )
				{
					TableCell tCellHeader = new TableCell();
					headerRow.Cells.Add( tCellHeader );
				}
				headerRow.CssClass = "bold";
				Table1.Rows.Add( headerRow );

				headerRow.Cells[0].Text = "Név";
				headerRow.Cells[1].Text = "Tantárgy";
				headerRow.Cells[2].Text = "Típus";
				headerRow.Cells[3].Text = "Kétheti óraszám";
				headerRow.Cells[4].Text = "Preferencia";


				for( int k = 0; k < tasks.Count; k++ )
				{
					if( tasks[k].PossibleTeachers.Where( ta => ta.Neptun == me.Neptun ).Any() )
					{

						TableRow row = new TableRow();
						for( int i = 0; i < colNbr; i++ )
						{
							TableCell cell = new TableCell();
							row.Cells.Add( cell );
						}

						row.Cells[0].Text = tasks[k].Name.Trim();
						row.Cells[1].Text = tasks[k].RelatedFact.Name.Trim();
						row.Cells[2].Text = tasks[k].Type.Name.Trim();
						row.Cells[3].Text = "" + tasks[k].GetHoursOnTwoAWeek() + " óra";


						starArray[k] = new DropDownList();
						starArray[k].Items.Add( "Semmiképp sem vállalom" );
						starArray[k].Items.Add( "Nagyon nem kedvelem" );
						starArray[k].Items.Add( "Nem kedvelem" );
						starArray[k].Items.Add( "Közömbös" );
						starArray[k].Items.Add( "Kedvelem" );
						starArray[k].Items.Add( "Nagyon kedvelem" );
						starArray[k].Items.Add( "Mindenképpen én tartom" );
						int stars = me.GetStars( tasks[k] );
						starArray[k].SelectedIndex = stars;

						row.Cells[4].Controls.Add( starArray[k] );
						Table1.Rows.Add( row );
					}
				}

				Table1.Style["position"] = "inherited";
				Table1.Style["Top"] = "20px";
				ContentPlaceHolder c = (ContentPlaceHolder)( Page.Form.FindControl( "MainContent" ) );

				c.Controls.Add( Table1 );
				Panel p = new Panel();
				p.Height = 50;
				p.Width = 100;
				c.Controls.Add( p );

				Button b = new Button();
				b.Style["position"] = "inherited";
				b.Style["Top"] = "20px";
				b.Text = "Mentés";
				b.Click += Button1_Click;
				c.Controls.Add( b );
			}
		}

		void PersonalPreferences_SelectedIndexChanged( object sender, EventArgs e )
		{

		}

		protected override void OnInit( EventArgs e )
		{
			base.OnInit( e );

			try
			{
				InitComponents();
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}

	}
}